package com.example.petshopping.ui.samecity;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.example.petshopping.R;
import com.example.petshopping.ui.main.MainActivity;
import com.example.petshopping.ui.pet.PetActivity;
import com.example.petshopping.ui.aquarium.AquariumActivity;
import com.example.petshopping.ui.fixedprice.FixedPriceActivity;
import com.example.chongwushangcheng.PetExchangeActivity;
import com.example.chongwushangcheng.activity.LocalPetStoreActivity;
import com.example.petshopping.ui.DoorToDoorServiceActivity;
import android.view.View;
import android.widget.ImageButton;
import com.google.android.material.tabs.TabLayout;

public class SameCityActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_same_city);

        // 设置状态栏高度
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            int statusBarHeight = getResources().getDimensionPixelSize(resourceId);
            findViewById(R.id.status_bar_bg).getLayoutParams().height = statusBarHeight;
        }

        setupNavigation();
        setupServiceButtons();
        
        // 搜索输入框点击跳转到搜索页
        findViewById(R.id.search_input).setOnClickListener(v -> {
            Intent intent = new Intent(SameCityActivity.this, com.example.petshopping.ui.search.SearchActivity.class);
            startActivity(intent);
        });
        
        // 设置底部导航栏
        setupBottomNavigation();
    }
    
    private void setupBottomNavigation() {
        // 获取底部导航栏
        com.google.android.material.bottomnavigation.BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
        
        // 设置当前选中项为首页(因为同城是顶部标签，底部没有对应标签)
        bottomNav.setSelectedItemId(R.id.navigation_home);
        
        // 设置点击监听器
        bottomNav.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    // 跳转到首页
                    startActivity(new Intent(this, MainActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
                case R.id.navigation_category:
                    // 跳转到分类页面
                    startActivity(new Intent(this, com.example.petshopping.ui.category.CategoryActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
                case R.id.navigation_message:
                    // 跳转到消息页面
                    startActivity(new Intent(this, com.example.petshopping.ui.message.MessageActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
                case R.id.navigation_profile:
                    // 跳转到我的页面
                    startActivity(new Intent(this, com.example.petshopping.ui.profile.ProfileActivity.class));
                    overridePendingTransition(0,0);
                    finish();
                    return true;
            }
            return false;
        });
        
        // 设置中间按钮点击事件
        findViewById(R.id.center_button).setOnClickListener(v -> {
            // 中间按钮点击事件
        });
        
        // 设置参与按钮点击事件
        findViewById(R.id.participate_btn).setOnClickListener(v -> {
            // 参与按钮点击事件
        });
    }

    private void setupServiceButtons() {
        // 设置宠物交流按钮点击事件
        ImageButton btnPetExchange = findViewById(R.id.btn_pet_exchange);
        btnPetExchange.setOnClickListener(v -> {
            Intent intent = new Intent(SameCityActivity.this, PetExchangeActivity.class);
            startActivity(intent);
            overridePendingTransition(0,0);
        });
        
        // 设置本地宠店按钮点击事件
        ImageButton btnLocalShop = findViewById(R.id.btn_local_shop);
        btnLocalShop.setOnClickListener(v -> {
            Intent intent = new Intent(SameCityActivity.this, com.example.chongwushangcheng.activity.LocalPetStoreActivity.class);
            startActivity(intent);
            overridePendingTransition(0,0);
        });
        
        // 设置鱼缸造景按钮点击事件
        ImageButton btnAquarium = findViewById(R.id.btn_aquarium);
        btnAquarium.setOnClickListener(v -> {
            Intent intent = new Intent(SameCityActivity.this, com.example.petshopping.ui.FishTankLandscapingRewardActivity.class);
            startActivity(intent);
            overridePendingTransition(0,0);
        });
        
        // 设置上门服务按钮点击事件
        ImageButton btnDoorService = findViewById(R.id.btn_door_service);
        btnDoorService.setOnClickListener(v -> {
            Intent intent = new Intent(SameCityActivity.this, DoorToDoorServiceActivity.class);
            startActivity(intent);
            overridePendingTransition(0,0);
        });
        
        // 其他服务按钮的点击事件可以按需添加
    }

    private void setupNavigation() {
        TabLayout navMenu = findViewById(R.id.nav_menu);
        navMenu.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                // 如果不是点击当前页面对应的标签，则进行页面跳转
                if (position != 4) { // 同城标签的位置是4
                    switch (position) {
                        case 0: // 首页
                            startActivity(new Intent(SameCityActivity.this, MainActivity.class));
                            overridePendingTransition(0,0);
                            finish();
                            break;
                        case 1: // 宠物
                            startActivity(new Intent(SameCityActivity.this, PetActivity.class));
                            overridePendingTransition(0,0);
                            finish();
                            break;
                        case 2: // 水族
                            startActivity(new Intent(SameCityActivity.this, AquariumActivity.class));
                            overridePendingTransition(0,0);
                            finish();
                            break;
                        case 3: // 一口价
                            startActivity(new Intent(SameCityActivity.this, FixedPriceActivity.class));
                            overridePendingTransition(0,0);
                            finish();
                            break;
                    }
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // 不需要处理
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 不需要处理
            }
        });
        // 设置默认选中的标签为同城
        navMenu.getTabAt(4).select();
    }
} 